Elastic Net Example

This is a simple example of Elastic Net using Python and scikit-learn.

Elastic Net Overview

Elastic Net is a linear regression model that combines the penalties of both L1 (Lasso) and L2 (Ridge) regularization. It is particularly useful when dealing with high-dimensional datasets where some features may be irrelevant or highly correlated. The elastic net penalty term allows for variable selection (sparsity) and handles the collinearity of features.

Key concepts of Elastic Net:

Elastic Net is commonly used in regression tasks where there is a need for regularization to prevent overfitting and handle correlated features.

Python Source Code:

# Import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.linear_model import ElasticNet
from sklearn.metrics import mean_squared_error

# Generate synthetic regression data
np.random.seed(42)
X, y = make_regression(n_samples=100, n_features=1, noise=20, random_state=42)

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Build an Elastic Net model
elastic_net = ElasticNet(alpha=1, l1_ratio=0.5, random_state=42)
elastic_net.fit(X_train, y_train)

# Make predictions on the test set
y_pred = elastic_net.predict(X_test)

# Evaluate the model
mse = mean_squared_error(y_test, y_pred)
print(f'Mean Squared Error: {mse}')

# Plot the results
plt.scatter(X_test, y_test, color='blue', label='Actual Data')
plt.plot(X_test, y_pred, color='red', label='Elastic Net Predictions')
plt.title('Elastic Net Example')
plt.xlabel('X')
plt.ylabel('y')
plt.legend()
plt.show()

Explanation: